﻿#ifndef LIB_CWEBUIMANAGER_H
#define LIB_CWEBUIMANAGER_H

#include <QObject>
#include <QLayout>
#include "../common/common.h"

#ifdef ENABLE_QCEFVIEW
#include "QCefContext.h"
#include "QCefView.h"
#else
#ifdef ENABLE_WEBENGINE
#include <QWebEngineView>
#endif
#include <QtWebChannel>
#endif

class CWebUIManager : public QObject
{
    Q_OBJECT

public:
    explicit CWebUIManager(QCoreApplication* app,int argc, char* argv[],
                           QObject *parent = nullptr);
    ~CWebUIManager();

    /// 初始化QCefView
    void init_QCefView(QCoreApplication* app,int argc, char* argv[]);

    ///打开一个url
    Q_INVOKABLE void openUrl(QLayout *pLayout,
                             QString url,
                             bool isShow=true,
                             bool isContentMenu=true,
                             QWidget *parent=nullptr);
    ///是否显示网页
    Q_INVOKABLE void setShow(bool isShow);
    ///重新载入网页
    Q_INVOKABLE void reLoadData(void);

    /// 发送数据给网页
    Q_INVOKABLE void sendDataToUrl(const QString data);

signals:
    /// 网页上处理qt中发送的数据
    void onProcessQtSendedData(const QString data);
    /// qt中处理网页上发送的数据
    void UrlSendedData(const QString data);

public slots:
    /// 处理从网页中发送过来的数据
    void sendDataToQt(const QString data);
    void onInvokeMethod(int browserId, qint64 frameId, const QString& method, const QVariantList& arguments);
    void onloadError(int browserId,
                   qint64 frameId,
                   bool isMainFrame,
                   int errorCode,
                   const QString& errorMsg,
                   const QString& failedUrl);
private:

#ifdef ENABLE_QCEFVIEW
    QCefContext *m_cefContext;
    QCefView *m_cefViewWidget;
#else
#ifdef ENABLE_WEBENGINE
    QWebEngineView m_webView;
    QWebChannel m_webChannel;
#endif
#endif
};

#endif // CWEBUIMANAGER_H
